package com.systemic.service.impl;

import com.systemic.service.UserInfoService;
import com.systemic.mapper.UserInfoMapper;
import com.systemic.pojo.UserInfo;

import com.systemic.utils.FileUtils;
import com.systemic.vo.PageVo;
import com.systemic.vo.UserItem;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.io.File;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName: UserInfoServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2023-12-27 14:38
 * @Version:
 **/
@Service
@PropertySource("classpath:/properties/image.properties")
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

//    @Value("${image.path}")
//    private String filesPath;    // 用户能够访问的虚拟路径

    @Value("${image.avatarUrl}")
    private String avatarUrl;

    @Value("${image.avatar}")
    private String avatarPath;        // 使用注解的方式定义文件地址

    @Value("${image.bgUrl}")
    private String bgUrl;

    @Value("${image.background}")
    private String backgroundPath;        // 使用注解的方式定义文件地址



    /**
     * 新增用户信息
     * @param userInfo
     */
    @Override
    public void addUserInfo(UserInfo userInfo) {
        Date date = new Date();
        userInfo.setGmtCreate(date);
        userInfo.setGmtModified(date);
        userInfoMapper.insert(userInfo);
    }

    /**
     * 修改用户信息业务
     * @param userInfo
     */
    @Override
    public void updateUserInfo(UserInfo userInfo) {
        Date date = new Date();
        userInfo.setGmtModified(date);
        userInfoMapper.update(userInfo);
    }

    /**
     * 获取用户信息
     * @param uid
     * @return
     */
    @Override
    public UserInfo selectUserInfoByUid(Integer uid) {
        return userInfoMapper.selectUserInfoByUid(uid);
    }



    /**
     * 上传头像
     * @param file
     * @param uid
     */
    @Override
    @Transactional
    public void uploadAvatar(MultipartFile file, Integer uid) {
        if (file!=null){
            try{
                String url = FileUtils.uploadImage(file, avatarPath, avatarUrl);
                UserInfo userInfo = new UserInfo();
                userInfo.setUid(uid);
                userInfo.setAvatar(url);
                userInfo.setGmtModified(new Date());
                userInfoMapper.update(userInfo);
            }catch (Exception e){
                throw new RuntimeException("出错"+e);
            }
        }

    }

    /**
     * 上传壁纸
     * @param file
     * @param uid
     */
    @Override
    @Transactional
    public void uploadBackground(MultipartFile file, Integer uid) {
        if (file!=null){
            try{
                String url = FileUtils.uploadImage(file, backgroundPath, bgUrl);
                UserInfo userInfo = new UserInfo();
                userInfo.setUid(uid);
                userInfo.setBackground(url);
                userInfo.setGmtModified(new Date());
                userInfoMapper.update(userInfo);
            } catch (Exception e) {
                throw new RuntimeException("上传失败" + e);
            }
        }

    }

    /**
     * 获取用户列表
     * @param userInfo
     * @return
     */
    @Override
    public List<UserInfo> select(UserInfo userInfo) {
        return userInfoMapper.selectList(userInfo);
    }


    // TODO:使用AOP实现功能扩展
    /**
     * 分页查询
     * @param pageVo
     * @return
     */
    @Override
    public List<UserInfo> selectListPage(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        return userInfoMapper.selectListPage(offset,pageSize);
    }

    /**
     * 查询用户item
     * @param uid
     * @return
     */
    @Override
    public UserItem selectUserItem(Integer uid) {
        return userInfoMapper.selectEntityById(uid);
    }


}
